from rest_framework import serializers
from staff.models import Employee
from .models import AttendanceRecord


class EmployeeBriefSerializer(serializers.ModelSerializer):
    """员工简要信息序列化器"""
    class Meta:
        model = Employee
        fields = ['id', 'work_id', 'name', 'department', 'position']


class AttendanceDetailSerializer(serializers.ModelSerializer):
    """考勤明细序列化器"""
    class Meta:
        model = AttendanceRecord
        fields = [
            'record_date', 'check_in_time', 'check_out_time',
            'status', 'late_minutes', 'early_leave_minutes',
            'is_corrected', 'correction_note'
        ]
        extra_kwargs = {
            'check_in_time': {'format': '%H:%M:%S'},
            'check_out_time': {'format': '%H:%M:%S'},
            'record_date': {'format': '%Y-%m-%d'}
        }


class CalendarDaySerializer(serializers.Serializer):
    """日历单日数据序列化器"""
    date = serializers.DateField(format='%Y-%m-%d')
    weekday = serializers.CharField()
    status = serializers.CharField()
    status_display = serializers.CharField()  # 状态中文显示
    check_in = serializers.TimeField(format='%H:%M:%S', allow_null=True)
    check_out = serializers.TimeField(format='%H:%M:%S', allow_null=True)
    note = serializers.CharField(allow_blank=True)


class AttendanceSummarySerializer(serializers.Serializer):
    """考勤汇总数据序列化器"""
    year = serializers.IntegerField()
    month = serializers.IntegerField()
    date_range = serializers.CharField()
    work_days = serializers.IntegerField()
    attendance_rate = serializers.FloatField()
    normal_count = serializers.IntegerField()
    abnormal_count = serializers.IntegerField()
    late_count = serializers.IntegerField()
    late_total_minutes = serializers.IntegerField()
    early_leave_count = serializers.IntegerField()
    missing_card_count = serializers.IntegerField()
    absent_count = serializers.IntegerField()
    leave_count = serializers.IntegerField()
    business_trip_count = serializers.IntegerField()


class MonthlyAttendanceResponseSerializer(serializers.Serializer):
    """月度考勤完整响应序列化器"""
    employee = EmployeeBriefSerializer()
    summary = AttendanceSummarySerializer()
    calendar = CalendarDaySerializer(many=True)
